package drds.server.sqlparser.sqlobject.statement.table;

import drds.server.sqlparser.sqlobject.expression.Expression;
import drds.server.sqlparser.visitor.Visitor;

/**
 * 
 * @author 曹正辉<330937205@qq.com>
 * 
 */
public class JoinTable extends AbstractTable {

	protected ITable left;
	protected JoinType joinType;
	protected ITable right;
	protected Expression on;

	public JoinTable(String alias) {
		super(alias);
	}

	public JoinTable() {

	}

	public void setLeft(ITable left) {
		if (left != null) {
			left.setParent(this);
		}
		this.left = left;
	}

	public ITable getLeft() {
		return this.left;
	}

	public void setJoinType(JoinType joinType) {
		this.joinType = joinType;
	}

	public JoinType getJoinType() {
		return this.joinType;
	}

	public void setRight(ITable right) {
		if (right != null) {
			right.setParent(this);
		}
		this.right = right;
	}

	public ITable getRight() {
		return this.right;
	}

	public void setOn(Expression on) {
		if (on != null) {
			on.setParent(this);
		}
		this.on = on;
	}

	public Expression getOn() {
		return this.on;
	}

	protected void accept0(Visitor visitor) {
		if (visitor.visit(this)) {
			acceptChild(visitor, this.left);
			acceptChild(visitor, this.right);
			acceptChild(visitor, this.on);
		}

		visitor.endVisit(this);
	}

	public void output(StringBuffer sb) {
		this.left.output(sb);
		sb.append(' ');
		sb.append(JoinType.toString(this.joinType));
		sb.append(' ');
		this.right.output(sb);

		if (this.on != null) {
			sb.append(" on ");
			this.on.output(sb);
		}
	}

	public static enum JoinType {
		JOIN("join"), //
		LEFT_JOIN("left join"), //
		RIGHT_JOIN("right join"); //

		public final String name;

		JoinType(String name) {
			this.name = name;
		}

		public static String toString(JoinType joinType) {
			return joinType.name;
		}
	}
}
